package com.arelikebrothers.demo.algorithm.tree.file;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * Created by lennon on 28/06/2017.
 */
public class ListDir {

    private void listDir(int depth, String path) {
        printName(depth, path);
        File file = new File(path);
        String[] list = file.list();
        Arrays.asList(list).forEach(fileName -> {
            File f = Paths.get(path, fileName).toFile();
            if (f.isDirectory()) {
                listDir(depth + 1, f.getAbsolutePath());
            } else {
                printName(depth + 1, f.getAbsolutePath());
            }
        });

    }

    private void printName(int depth, String path) {

        String name = new File(path).getName();

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < depth; i++) {
            sb.append("    ");
        }
        sb.append(name);
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        new ListDir().listDir(0, "/Users/admin/Downloads");
    }
}
